package com.example.mapper;

import com.example.pojo.Article;
import com.example.pojo.Category;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleMapper {
    //新增文章
    // 查找最小可用ID（优化版）
    @Select("""
        SELECT MIN(t1.id + 1) 
        FROM article t1 
        LEFT JOIN article t2 ON t1.id + 1 = t2.id 
        WHERE t2.id IS NULL 
        AND t1.id < 999999
        """)
    Integer findFirstAvailableId();

    // 获取当前最大ID
    @Select("SELECT COALESCE(MAX(id), 0) FROM article")
    Integer findMaxId();

    // 新增文章（强制指定ID）
    @Insert("""
        INSERT INTO article(
            id, title, content, cover_img, 
            state, category_id, 
            create_user, create_time, update_time
        ) VALUES(
            #{id}, #{title}, #{content}, #{coverImg},
            #{state}, #{categoryId},
            #{createUser}, #{createTime}, #{updateTime}
        )
        """)
    int add(Article article);

    //获取文章列表
    List<Article> list(Integer userId, Integer categoryId, String state);

    //更新文章
    @Update("update article set title=#{title},content=#{content},cover_img=#{coverImg},state=#{state},category_id=#{categoryId} " +
            "where id=#{id}")
    void update(Article article);

    //根据id查看文章详情
    @Select("select * from article where id = #{id}")
    Article findById(Integer id);

    //删除文章
    @Delete("DELETE FROM article WHERE id = #{id}")
    void delete(Integer id);
}
